package com.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;

import java.io.FileOutputStream;

/**
 * @User xiangyl
 * @Data 2021/1/10
 */
public class MyPdfExport {

    // 定义全局的字体静态变量
    private static Font titlefont;
    private static Font headfont;
    private static Font keyfont;
    private static Font textfont;
    // 最大宽度
    private static int maxWidth = 520;
    // 静态代码块
    static {
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont bfChinese = BaseFont.createFont();
            titlefont = new Font(bfChinese, 16, Font.BOLD);
            headfont = new Font(bfChinese, 14, Font.BOLD);
            keyfont = new Font(bfChinese, 10, Font.BOLD);
            textfont = new Font(bfChinese, 10, Font.NORMAL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) throws Exception {
        Document document = new Document(PageSize.A4, 10, 10, 10, 10);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("D:\\test\\myPdf.pdf"));
        document.open();
        generatePDF(document,pdfWriter);
        document.close();
    }

    public static void generatePDF (Document document,PdfWriter pdfWriter)throws Exception{
        Paragraph paragraph = new Paragraph("2021 come on");
        //设置文字居中方式；靠左：0，居中：1，靠右2
        paragraph.setAlignment(2);
        //当前段落后的距离，跟下一个段落前的距离有相同的作用
        paragraph.setSpacingAfter(100);
        document.add(paragraph);
        Paragraph come_on_forever = new Paragraph("come on forever");
        come_on_forever.setAlignment(2);
        come_on_forever.setSpacingBefore(100);
        document.add(come_on_forever);
        StringBuilder stringBuilder = new StringBuilder("come on ");
        for (int i = 0; i < 50; i++) {
            stringBuilder.append("come on ");
        }
        Paragraph elements = new Paragraph(stringBuilder.toString());
        document.add(elements);
        Paragraph paragraph1 = new Paragraph();
        paragraph1.add(new Chunk(new LineSeparator()));
        //点线，由连续的点构成的线
        paragraph1.add(new Chunk(new DottedLineSeparator()));
        paragraph1.add(new Chunk(new LineSeparator()));
        document.add(paragraph1);
        Anchor baidu = new Anchor("baidu");
        baidu.setReference("www.baidu.com");
        document.add(baidu);
        Image instance = Image.getInstance("E:\\myFile\\Picture\\1.jpg");
        instance.setAlignment(Image.ALIGN_CENTER);
        instance.scalePercent(1);
        document.add(instance);
        PdfPTable table = createTable(new float[]{520, 1040});
        table.addCell(createCell("hellopdf",new Font(),Element.ALIGN_LEFT));
        table.addCell(createCell("hellopdf",new Font(),Element.ALIGN_LEFT));
        table.addCell(createCell("hellopdf",new Font(),Element.ALIGN_LEFT));
        table.addCell(createCell("hellopdf",new Font(),Element.ALIGN_LEFT));
        document.add(table);
        System.out.println(pdfWriter.getCurrentPageNumber());
        document.newPage();
        PdfPTable table2 = createTable(new float[]{520, 1040});
        table2.addCell(createCell("hellopdf",new Font(),Element.ALIGN_LEFT));
        table2.addCell(createCell("hellopdf",new Font(),Element.ALIGN_LEFT));
        document.add(table2);
        System.out.println(pdfWriter.getPageNumber());

    }


    public static PdfPTable createTable(float[] widths){
        PdfPTable pdfPTable = new PdfPTable(widths);
        try {
            pdfPTable.setTotalWidth(520);
            pdfPTable.setLockedWidth(true);
            //设置水平对齐方式，参照物为上一层对象，谁add这个table对象就以谁为参照物
            pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pdfPTable;
    }


    public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setPadding(3.0f);
        if (!boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(15.0f);
            cell.setPaddingBottom(8.0f);
        } else if (boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(0.0f);
            cell.setPaddingBottom(15.0f);
        }
        return cell;
    }

    public static PdfPCell createCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setFixedHeight(100f);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
        cell.setPaddingLeft(0f);
        return cell;
    }

}
